package com.sunny.ynhz.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.api.okhttp.OkHttpHelper;
import com.sunny.ynhz.bean.WarpFileMsg;
import com.sunny.ynhz.servicebestpractice.DownloadListener;
import com.sunny.ynhz.servicebestpractice.DownloadTask;
import com.sunny.ynhz.utils.FileUtils;
import com.sunny.ynhz.utils.OpenFileUtils;
import com.sunny.ynhz.utils.SPUtil;

import java.io.File;
import java.util.List;

/**
 * Created by huhuan123 on 2018/6/1.
 */

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> {
    Context context;
    List<WarpFileMsg> msgList;
    SparseArray<String> mSparseArray = new SparseArray<>();
    LinearLayoutManager linearLayoutManager;

    public FileAdapter(Context context, List<WarpFileMsg> msgList, LinearLayoutManager linearLayoutManager) {
        this.context = context;
        this.msgList = msgList;
        this.linearLayoutManager = linearLayoutManager;
    }

    @Override
    public FileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_file_progress, parent, false));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final int mPosition = position;
        WarpFileMsg warpFileMsg = msgList.get(position);
        holder.tv_file_time.setText(warpFileMsg.getFileTime());
        holder.tv_file_name.setText(warpFileMsg.getFileName());
        holder.tv_file_option.setText(warpFileMsg.getType());
        if (position != 0) {
            holder.tv_file_option.setBackground(context.getDrawable(R.drawable.corner_border));
            holder.tv_file_option.setTextColor(Color.parseColor("#69A5FE"));

        } else {
            holder.tv_file_option.setBackground(context.getDrawable(R.drawable.corner_border_tran));
            holder.tv_file_option.setTextColor(Color.parseColor("#666666"));
        }
        int loading = warpFileMsg.getLoading();
        if (loading == 0) {
            holder.tv_file_option.setText("下载");
            holder.tv_file_option.setClickable(true);
        } else if (loading == 1) {
            holder.tv_file_option.setText(warpFileMsg.getProgress() + "%");
            holder.tv_file_option.setClickable(true);
        } else if (loading == -1) {
            holder.tv_file_option.setText("操作");
            holder.tv_file_option.setClickable(false);
        } else {
            holder.tv_file_option.setText("查看");
            holder.tv_file_option.setClickable(true);
        }
        holder.tv_file_option.setOnClickListener(new View.OnClickListener() {
            private DownloadTask downloadTask;

            @Override
            public void onClick(View view) {
                if (warpFileMsg.getLoadUrl() == null) {
                    return;
                }
                if (warpFileMsg.getLoading() == 2) {
                    String string = getloadedFile(warpFileMsg.getLoadUrl());
                    if (string.contains(".doc")) {
                        OpenFileUtils.startOpenWordFile(string, context);
                    } else if (string.contains(".pdf")) {
                        OpenFileUtils.startOpenpdfFile(string, context);
                    } else if (string.contains("xls")) {
                        OpenFileUtils.startOpenExcelFile(string, context);
                    }
                    return;
                }
                handlerDownLoad();
            }

            private void handlerDownLoad() {
                if (mSparseArray.get(position) == null) {
                    mSparseArray.put(position, warpFileMsg.getLoadUrl());
                    downloadTask = new DownloadTask(new DownloadListener() {
                        @Override
                        public void onProgress(int progress) {
                            if (progress == 100) {
                                warpFileMsg.setLoading(2);
                                holder.tv_file_option.setText("查看");
                            } else {
                                warpFileMsg.setLoading(1);
                            }
                            warpFileMsg.setProgress(progress);
                            int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                            int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                            if (firstVisibleItemPosition <= mPosition && mPosition <= lastVisibleItemPosition) {
                                int realPosition = mPosition - firstVisibleItemPosition;
                                View currentView = linearLayoutManager.getChildAt(realPosition);
                                TextView tv_file_option = currentView.findViewById(R.id.tv_file_option);
                                tv_file_option.setText(progress + "%");
                                if(progress==100){
                                    warpFileMsg.setLoading(2);
                                    holder.tv_file_option.setText("查看");
                                }
                            }
                        }

                        @Override
                        public void onSuccess() {
                        }

                        @Override
                        public void onFailed() {
                            holder.tv_file_option.setText(warpFileMsg.getProgress()+"%");
                            warpFileMsg.setLoading(0);

                        }

                        @Override
                        public void onPaused() {
                            warpFileMsg.setLoading(0);
                            holder.tv_file_option.setText(warpFileMsg.getProgress()+"%");
                        }

                        @Override
                        public void onCanceled() {

                        }
                    });
                    downloadTask.execute(warpFileMsg.getLoadUrl());
                } else {
                    mSparseArray.remove(position);
                    downloadTask.pauseDownload();
                    holder.tv_file_option.setText(warpFileMsg.getProgress()+"%");
                    warpFileMsg.setLoading(0);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return msgList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_file_name;
        TextView tv_file_time;
        TextView tv_file_option;

        //在布局中找到所含有的UI组件
        public ViewHolder(View itemView) {
            super(itemView);
            tv_file_name = itemView.findViewById(R.id.tv_file_name);
            tv_file_time = itemView.findViewById(R.id.tv_file_time);
            tv_file_option = itemView.findViewById(R.id.tv_file_option);

        }
    }

    public String getloadedFile(String downloadUrl) {
        String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
        //下载文件存放的目录
        String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
        return directory + fileName;

    }
}


